<template>
  <div class="adminlogin">
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form" size="large">
      <div class="loginhidden">
        <div class="title">欢迎使用华坤管理系统</div>
        <el-button type="primary" text size="large" @click="tabHandle" style="font-size: 20px;">
          切换<el-icon class="right">
            <ArrowRight />
          </el-icon>
        </el-button>
      </div>

      <el-form-item prop="username">
        <el-input v-model="loginForm.username" placeholder="请输入用户名">
          <template #prefix>
            <div class="loginprefix"><img src="../assets/images/userlog.png" alt=""></div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
          @keyup.enter.native="handleLogin">
          <template #prefix>
            <div class="loginprefix"><img src="../assets/images/passwordlog.png" alt=""></div>
          </template>
        </el-input>
      </el-form-item>

      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="large" type="primary"
          style="width:100%;background-color: #1F63FF;height:53px;border-radius: 50px;"
          @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import Cookies from "js-cookie";
import useUserStore from '@/store/modules/user'
import { onMounted, reactive } from "vue";

const userStore = useUserStore()
const router = useRouter();
const { proxy } = getCurrentInstance();
const loginForm = reactive({
  username: "admin",
  password: "admin",
});

const loginRules = {
  username: [
    { required: true, trigger: "blur", message: "请输入您的账号" },
    { min: 2, max: 20, message: "长度在 2 到 20 个字符", trigger: "blur" }
  ],
  password: [
    { required: true, trigger: "blur", message: "请输入您的密码" },
    { min: 5, max: 20, message: "长度在 5 到 20 个字符", trigger: "blur" }
  ],
};

const companylist = ref([])

const codeUrl = ref("");
const loading = ref(false);
// 验证码开关
const captchaEnabled = ref(true);
// 注册开关
const register = ref(false);
const redirect = ref(undefined);

function handleLogin() {
  proxy.$refs.loginRef.validate(valid => {
    if (valid) {
      loading.value = true;
      // 调用action的登录方法
      userStore.login(loginForm).then(() => {
        console.log(router.getRoutes())
        router.push({ path: "/user" });
      }).catch(() => {
        loading.value = false;

      });
    }
  });
}


function tabHandle() {
  router.push({ path: "/login" });
}

</script>

<style>
.adminlogin .el-input__inner {
  font-size: 20px;
  display: flex;
  align-items: center;
}

.adminlogin .el-input__prefix {
  display: flex;
}

.adminlogin .el-input__wrapper {
  border-bottom: 1px solid var(--el-input-border-color, var(--el-border-color));
  box-shadow: 0 0 0 0;
}

.adminlogin .el-form-item.is-error .el-input__wrapper {
  box-shadow: 0 0 0 0;
}

.adminlogin .el-form-item {
  margin: 60px 0;
}
</style>
<style lang='scss' scoped>
.adminlogin {
  height: 100%;
  background-image: url("../assets/images/loginBack.png");
  background-size: 100% 100%;
  min-width: 1580px;
  min-height: 880px;
  overflow: hidden;
}

.loginhidden {
  display: flex;
  align-items: center
}

.title {
  color: #333333;
  font-size: 30px;
  font-weight: 600;
}

.login-form {
  border-radius: 6px;
  width: 464px;
  padding: 25px 25px 5px 25px;
  margin-left: 50%;
  margin-top: 12%;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.loginprefix {
  display: flex;
  align-items: center;
  margin-right: 20px;

  img {
    width: 30px;
    height: 29px;
  }
}
</style>